<?php

namespace app\admin\controller;

use My\Redis;
use think\Controller;
use think\Db;
use think\Cookie;
use think\Session;
use think\Request;

class Login extends Controller
{
    //初始化的方法
    public function _initialize()
    {

        //取得请求数据
        $request = Request::instance();
        //获取模块名
        $mname = strtolower($request->module());
        //获取控制器名
        $cname = strtolower($request->controller());
         //获取方法名
        $aname = strtolower($request->action());

        // 如果正在请求登录，不再进行判断【防止死循环】
        if($mname == 'admin' && $cname == 'login'){
            return ;
        }

        //判断是否登录
       if(!Session::has('admin_name')){
            return $this->redirect('admin/login/loginindex');
        }

        // 如果正在请求index，不进行RBAC判定
        if($mname == 'admin' && $cname == 'index'){
            return ;
        }

        $m = '"'.$mname.'"';
        $mc = '"'.$mname.'/'.$cname.'"';
        $mca = '"'.$mname.'/'.$cname.'/'.$aname.'"';
        // return var_dump($mca);
        $list = Session::get('nodelist'); //获取权限列表
        //让超级管理员admin拥有所有权限
        if(Session::get('admin_name') != 'admin'){
            //验证操作权限
            if(!(stripos($list,$m) || stripos($list,$mc) || stripos($list,$mca))){
                return $this->error("抱歉！没有操作权限");
            }

        }

    }

    //进行登录判断
    public function loginindex()
    {
        if ( Cookie::get('admin_name') && Cookie::get('admin_id')) {
            Session::set('admin_name',Cookie::get('admin_name'));
            if (Redis::get('admin_tocken'.Cookie::get('admin_id'))==Cookie::get('admin_tocken') ) {
                return $this->success('自动登录成功,跳转主页',url('admin/index/index'));
            }else{
                Session::clear();
                Cookie::clear();
                return $this->error('请重新登录',url('admin/login/index'));
            }
        }else{
            return $this->error('请登录',url('admin/login/index'));
        }
    }

    //加载登录页
    public function index()
    {
        return view('login/login',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
        ]);
    }

    // 执行登陆
    public function doLogin(Request $request)
    {
        $post = $request->post();
        $name = $post['name'];
        $password = $post['password'];
        //去数据库查询是否有此用户
        $data = Db::name('admin a')->join('gqx_user_role ur','ur.user_id=a.id','left')->join('gqx_role r','r.id=ur.role_id')->field('r.status as rolestatus,a.status,a.id,a.name,a.password')->where(['a.name'=>$name])->find();
        if (!empty($data['name'])){
            //判断密码
            if (md5($password) == $data['password']){
                //储存session,登录成功
                Session::set('admin_name',$data['name']);
                Session::set('admin_id',$data['id']);
                //密码相同时,判断是否记住密码
                if (!empty($post['remember'])){
                    //如果勾选记住密码,则存储cookie,jwt操作
                    $tocken = md5(rand()).date('YmdHis');
                    Cookie::set('admin_name',$data['name']);
                    Cookie::set('admin_id',$data['id']);
                    Cookie::set('admin_tocken',$tocken,604800);
                    //配置redis
                    Redis::set('admin_tocken'.$data['id'],$tocken);
                }

            }else{
                return $this->error('登录信息有误');
            }
        }else{
            //如果没有此用户
            return $this->error('登录信息有误');
        }
        if ($data['status'] != 1){
            return $this->error('该用户已被禁用');
        }

        //判断角色是否没有被禁用
        if ($data['rolestatus'] == 1){
            //上面判断完成之后,直接把权限遍历出来
            $nodelist = Db::name('user_role ur')->join('gqx_role_access ra','ra.role_id=ur.role_id')->join('gqx_access a','a.id=ra.access_id')->where(['ur.user_id'=>$data['id']])->field('a.module')->select();
            $nodelist=serialize($nodelist);
            Session::set('nodelist',$nodelist);
        }
        //登录成功,跳转首页
        return $this->success('登录成功,跳转主页',url('admin/index/index'));
    }
    
    //执行退出
    public function outLogin()
    {
        Session::clear();
        Cookie::delete('admin_tocken');
        Cookie::delete('admin_name');
        Cookie::delete('admin_id');
        return $this->error('退出成功,返回登录页',url('admin/login/index'));
    }


}
